#ifndef Smart_First
#define Smart_First

#include "Types.h"

#define SmartDef(name) \
class _NAME2_(name,Ptr) { \
	int died; \
	class name *g; \
	name *make(); \
public: \
	_NAME2_(name,Ptr)() \
		{ g= 0; died= 0; } \
	~_NAME2_(name,Ptr)(); \
	name *operator -> () \
		{ return make(); } \
	operator name* () \
		{ return make(); } \
}; \
extern _NAME2_(name,Ptr)

#define _SmartImpl(name,expr) \
_NAME2_(name,Ptr)::~_NAME2_(name,Ptr)() \
{ \
	if (g) delete g; died= 1; \
}; \
name *_NAME2_(name,Ptr)::make() \
{ \
	if (g) \
		return g; \
	if (died) { \
		fprintf(stderr, "error\n"); \
		return 0; \
	} \
	return g= expr; \
}; \
_NAME2_(name,Ptr)

#define SmartImpl(name) _SmartImpl(name,new name)

#endif
